package com.fancy.learncenter.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.MsgCommentAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.MsgCommentDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.view.CustomFootView;
import com.fancy.learncenter.view.CustomHeaderView;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MsgCommentActivity extends BaseActivity {

    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;


    MsgCommentAdapter msgCommentAdapter;
    @Bind(R.id.prompt_img)
    ImageView promptImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_comment, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("评论我的消息");
        msgCommentAdapter = new MsgCommentAdapter(this, new ArrayList<MsgCommentDataBean>());

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        recycleView.setAdapter(msgCommentAdapter);
        msgCommentAdapter.setCustomLoadMoreView(new CustomFootView(this));
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setCustomHeaderView(new CustomHeaderView(this));
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                LogUtil.MyLog("onLoadMore", "====onRefresh==");
                getDatas(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getDatas(false);
                LogUtil.MyLog("onLoadMore", "====onLoadMore==");
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });

        getDatas(true);
    }


    int page = 0;

    private void getDatas(final boolean isRefresh) {

        Map map = new HashMap();

        if (isRefresh) {
            page = 0;
        } else {
            page++;
        }
        map.put("page", page + "");

        HttpMehtod.getInstance().msgComment(map, new IdeaObserver<BaseDataBean<List<MsgCommentDataBean>>>() {
            @Override
            public void onSuccess(BaseDataBean<List<MsgCommentDataBean>> listBaseDataBean) {


                if (isRefresh) {
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);

                    if (listBaseDataBean.getData() == null || listBaseDataBean.getData().size() == 0) {
                        msgCommentAdapter.notifyDataSetChanged(new ArrayList<MsgCommentDataBean>());
                        promptImg.setVisibility(View.VISIBLE);
                        return;
                    } else {
                        promptImg.setVisibility(View.GONE);
                        msgCommentAdapter.notifyDataSetChanged((ArrayList<MsgCommentDataBean>) listBaseDataBean.getData());
                    }


                } else {
                    if (listBaseDataBean.getData() == null) {
                        xRefreshView.setLoadComplete(true);
                    } else if (listBaseDataBean.getData() != null && listBaseDataBean.getData().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.stopLoadMore();
                    }


                    if (listBaseDataBean.getData() == null) {
                        return;
                    }
                    msgCommentAdapter.addAll(listBaseDataBean.getData());
                }
            }

            @Override
            public void onFail(int errorCode) {
                xRefreshView.stopRefresh();
                xRefreshView.setLoadComplete(false);
            }
        });
    }
}
